/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { beforeAll, describe, expect, test } from 'vitest';
import { CHECK_MODE, testCaseCheck } from './common/testCommon';
import path from 'path';
import { Rule } from '../../../src/Index';
import { CheckEntry } from '../../../src/utils/common/CheckEntry';
import { CustomComponentCheck } from '../../../src/checker/performance/CustomComponentCheck';

let realPath: string = '';
let checkEntry: CheckEntry;

beforeAll(async () => {
    const rule: Rule = new Rule('');
    checkEntry = await testCaseCheck('./test/unittest/sample/CustomComponent', rule, CHECK_MODE.FILE2CHECK, CustomComponentCheck);
    realPath = checkEntry.scene.getRealProjectDir();
});

describe('CustomComponentCheckTest', () => {
    test('CustomComponentCheckTest_Report', () => {
        const detectFile: string = path.join(realPath, 'ets', 'CustomComponentReport.ets');
        let detectFileReports = checkEntry.fileChecks
            .filter((fileCheck) => fileCheck.arkFile.getFilePath() === detectFile)
            .flatMap((fileCheck) => fileCheck.issues);
        // 期望的报告数量和位置（需根据实际检测结果调整）
        const expectReportList = [
            '6%12%17%',
            '19%13%20%'
        ];
        expect(detectFileReports.length).toBe(expectReportList.length);
        // 打印实际的 fixKey 用于调试
        console.log('实际 fixKey:', detectFileReports.map(issue => issue.defect.fixKey));
        detectFileReports.forEach((issue, index) => {
            expect(issue.defect.fixKey).toContain(expectReportList[index]);
        });
    });

    test('CustomComponentCheckTest_NoReport', () => {
        const detectFile: string = path.join(realPath, 'ets', 'CustomComponentNoReport.ets');
        let detectFileReports = checkEntry.fileChecks
            .filter((fileCheck) => fileCheck.arkFile.getFilePath() === detectFile)
            .flatMap((fileCheck) => fileCheck.issues);
        expect(detectFileReports.length).toBe(0);
    });

    test('CustomComponentCheckTest_Report1', () => {
        const detectFile: string = path.join(realPath, 'ets', 'CustomComponentReport1.ets');
        let detectFileReports = checkEntry.fileChecks
            .filter((fileCheck) => fileCheck.arkFile.getFilePath() === detectFile)
            .flatMap((fileCheck) => fileCheck.issues);
        // 打印实际的 fixKey 用于调试
        console.log('CustomComponentReport1.ets 实际 fixKey:', detectFileReports.map(issue => issue.defect.fixKey));
        // 校验期望的fixKey
        const expectReportList = [
            '15%13%27%'
        ];
        expect(detectFileReports.length).toBe(expectReportList.length);
        detectFileReports.forEach((issue, index) => {
            expect(issue.defect.fixKey).toContain(expectReportList[index]);
        });
    });

    test('CustomComponentCheckTest_NoReport1', () => {
        const detectFile: string = path.join(realPath, 'ets', 'CustomComponentNoReport1.ets');
        let detectFileReports = checkEntry.fileChecks
            .filter((fileCheck) => fileCheck.arkFile.getFilePath() === detectFile)
            .flatMap((fileCheck) => fileCheck.issues);
        expect(detectFileReports.length).toBe(0);
    });
});
